$(function(){ 
	$('.table-sort').dataTable(pageStting(baseURL));
	$('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
	$(window).resize(function(){
		$(".table-sort").css('width','100%');
	})
});
// 从服务器分批取数据
 pageStting =  function(baseURL){
		var optsortable = {
					"bPaginate":true,
					"bProcessing":true,
					"stateSave":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/member/list'+"?t="+new Date().getTime(),
					"sPaginationType":'full_numbers',
					"lengthMenu":[10,25],
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [  
				                      {
				                    	"bVisible":false,
				                    	"aTargets":[0]
				                      },
				                      {               
		                            "render": function (data, type, full){  
		                            	if(full[2]!="论坛"){
		                            		var edit_herf ="<div class='text-c handlePointer'><img title='编辑用户' alt='编辑用户' src='"+baseURL+"/images/admin/cp_edite.png' onclick=layer_show('850','','编辑用户','"+baseURL+"/api/member/page/edit/"+full[0]+"') />"
		                            	}else {
		                            		var edit_herf ="<div class='text-c handlePointer'><img title='查看用户' alt='查看用户' src='"+baseURL+"/images/admin/cp_cash.png' onclick=layer_show('850','','查看用户','"+baseURL+"/api/member/page/showEngineerDetail/"+full[0]+"') />"
		                            		}
		                            	return edit_herf;
		                              },
		                            "aTargets":[8]
		                           }
				                   ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
								if($("#attrName").val()=="SOURCE"){
									searchConditions.queryName=$.trim($("#attrName").val());
									searchConditions.queryValue=$("#sou").val();
								}else if($.trim($("#userName").val())!=''){
									searchConditions.queryName=$.trim($("#attrName").val());
									searchConditions.queryValue=$("#userName").val();
								}

							if($("#projectSelect").val()!='' && $("#projectSelect").val()!=' '){
								searchConditions.source=$("#projectSelect").val();
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递
						        success: function(result) {
					    			if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										// json重构数组对象
										$.each(data,function(i,val){
											aaData[i]=[];
											aaData[i].push(val.id==null?'/':val.id);
											aaData[i].push(val.uid==null?'/':val.uid);
											aaData[i].push(engineerSource(val.source));
											aaData[i].push(val.username==null?'/':val.username);
											aaData[i].push(val.name==null?'/':val.name);
											aaData[i].push(val.mobile==null?'/':val.mobile);
											aaData[i].push(val.company==null?'/':val.company);
											aaData[i].push(val.projectCount==null?'/':val.projectCount);
											aaData[i].push(val.null_n);
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}					
doSearch = function(){
	   var seachValue={};
	   $('.table-sort').DataTable().search(seachValue).draw();
}	
doReset=function(){
	$('#userName').val('');
}
function engineerSource(obj){
	if(obj=="1"){
		return "论坛";
	}else return "咨询";
}